/*
 *  Copyright (C) 2005-2007 Renier Roth
 *  www.renier.de
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.renier.jkeepass.dialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.text.DateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import net.sf.nachocalendar.components.DateField;
import de.renier.jkeepass.Messages;
import de.renier.jkeepass.components.SymbolComboBox;
import de.renier.jkeepass.elements.BasicElement;
import de.renier.jkeepass.util.IconPool;
import de.renier.jkeepass.util.Utils;


/**
 * BasicElementDialog
 *
 * @author <a href="mailto:software@renier.de">Renier Roth</a>
 */
public class BasicElementDialog extends JDialog {

  
  private static final long serialVersionUID = 1L;
  private static final ImageIcon ICON_PASS_SHOW = new ImageIcon(BasicElementDialog.class.getResource("/org/javalobby/icons/20x20/UnLock.gif"));  //  @jve:decl-index=0: //$NON-NLS-1$
  private static final ImageIcon ICON_PASS_HIDE = new ImageIcon(BasicElementDialog.class.getResource("/org/javalobby/icons/20x20/Lock.gif"));  //  @jve:decl-index=0: //$NON-NLS-1$
  
  public static final int RESULT_CANCEL = 0;
  public static final int RESULT_SAVE = 1;
  
  private JPanel jContentPane = null;
  private JPanel jPanel = null;
  private JButton submitButton = null;
  private JButton cancelButton = null;
  private JPanel mainPanel = null;
  private JLabel nameLabel = null;
  private JTextField nameTextField = null;
  private JLabel userLabel = null;
  private JLabel passwordLabel = null;
  private JLabel urlLabel = null;
  private JLabel commentLabel = null;
  private JLabel expireLabel = null;
  private JTextField userTextField = null;
  private JTextField urlTextField = null;
  private JTextArea commentTextArea = null;
  private JPasswordField passwordPasswordField = null;
  private JLabel symbolLabel = null;
  private JComboBox symbolComboBox = null;
  private JButton tooglePasswordButton = null;
  private JLabel spaceLabel = null;
  private boolean passwordVisible = false;
  private JScrollPane commentScrollPane = null;
  private BasicElement basicElement = null;  //  @jve:decl-index=0:
  private int result = RESULT_CANCEL;
  private DateField dateField = null;
  private JCheckBox dateExpireCheckBox = null;
  private JButton addToClipboardButton = null;
  
  /**
   * @param owner
   */
  public BasicElementDialog(Frame owner) {
    super(owner);
    initialize();
  }

  /**
   * This method initializes this
   * 
   * @return void
   */
  private void initialize() {
    this.setSize(480, 480);
    this.setTitle(Messages.getString("BasicElementDialog.2")); //$NON-NLS-1$
    this.setModal(true);
    this.setContentPane(getJContentPane());
    // center dialog
    this.setLocationRelativeTo(getOwner());
    getRootPane().setDefaultButton(getSubmitButton());
  }

  /**
   * This method initializes jContentPane
   * 
   * @return javax.swing.JPanel
   */
  private JPanel getJContentPane() {
    if (jContentPane == null) {
      jContentPane = new JPanel();
      jContentPane.setLayout(new BorderLayout());
      jContentPane.add(getJPanel(), BorderLayout.SOUTH);
      jContentPane.add(getMainPanel(), BorderLayout.CENTER);
    }
    return jContentPane;
  }

  /**
   * This method initializes jPanel	
   * 	
   * @return javax.swing.JPanel	
   */
  private JPanel getJPanel() {
    if (jPanel == null) {
      jPanel = new JPanel();
      jPanel.setLayout(new FlowLayout());
      jPanel.add(getSubmitButton(), null);
      jPanel.add(getCancelButton(), null);
      jPanel.add(getAddToClipboardButton(), null);
    }
    return jPanel;
  }

  /**
   * This method initializes submitButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getSubmitButton() {
    if (submitButton == null) {
      submitButton = new JButton();
      submitButton.setText(Messages.getString("BasicElementDialog.3")); //$NON-NLS-1$
      submitButton.setIcon(new ImageIcon(getClass().getResource("/org/javalobby/icons/20x20/DataStore.gif"))); //$NON-NLS-1$
      submitButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          result = RESULT_SAVE;
          dispose();
        }
      });
    }
    return submitButton;
  }

  /**
   * This method initializes cancelButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getCancelButton() {
    if (cancelButton == null) {
      cancelButton = new JButton();
      cancelButton.setText(Messages.getString("BasicElementDialog.5")); //$NON-NLS-1$
      cancelButton.setIcon(new ImageIcon(getClass().getResource("/org/javalobby/icons/20x20/Delete.gif"))); //$NON-NLS-1$
      cancelButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          result = RESULT_CANCEL;
          dispose();
        }
      });
    }
    return cancelButton;
  }

  /**
   * This method initializes mainPanel	
   * 	
   * @return javax.swing.JPanel	
   */
  private JPanel getMainPanel() {
    if (mainPanel == null) {
      GridBagConstraints gridBagConstraints22 = new GridBagConstraints();
      gridBagConstraints22.gridx = 2;
      gridBagConstraints22.anchor = GridBagConstraints.WEST;
      gridBagConstraints22.gridy = 6;
      GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
      gridBagConstraints15.gridx = 1;
      gridBagConstraints15.fill = GridBagConstraints.NONE;
      gridBagConstraints15.anchor = GridBagConstraints.WEST;
      gridBagConstraints15.gridy = 6;
      GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
      gridBagConstraints31.fill = GridBagConstraints.BOTH;
      gridBagConstraints31.weighty = 1.0;
      gridBagConstraints31.gridx = 1;
      gridBagConstraints31.gridy = 5;
      gridBagConstraints31.gridwidth = 3;
      gridBagConstraints31.insets = new Insets(5, 0, 5, 10);
      gridBagConstraints31.weightx = 1.0;
      GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
      gridBagConstraints21.gridx = 0;
      gridBagConstraints21.insets = new Insets(5, 0, 0, 0);
      gridBagConstraints21.gridy = 0;
      spaceLabel = new JLabel();
      spaceLabel.setText(""); //$NON-NLS-1$
      GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
      gridBagConstraints14.gridx = 3;
      gridBagConstraints14.anchor = GridBagConstraints.WEST;
      gridBagConstraints14.gridwidth = 1;
      gridBagConstraints14.insets = new Insets(0, 0, 0, 10);
      gridBagConstraints14.gridy = 3;
      GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
      gridBagConstraints13.fill = GridBagConstraints.NONE;
      gridBagConstraints13.gridy = 1;
      gridBagConstraints13.weightx = 0.0D;
      gridBagConstraints13.anchor = GridBagConstraints.WEST;
      gridBagConstraints13.gridwidth = 1;
      gridBagConstraints13.insets = new Insets(0, 0, 0, 10);
      gridBagConstraints13.gridx = 3;
      GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
      gridBagConstraints12.gridx = 2;
      gridBagConstraints12.fill = GridBagConstraints.NONE;
      gridBagConstraints12.insets = new Insets(0, 10, 0, 10);
      gridBagConstraints12.anchor = GridBagConstraints.WEST;
      gridBagConstraints12.gridy = 1;
      symbolLabel = new JLabel();
      symbolLabel.setText(Messages.getString("BasicElementDialog.8")); //$NON-NLS-1$
      GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
      gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
      gridBagConstraints11.gridy = 3;
      gridBagConstraints11.weightx = 1.0;
      gridBagConstraints11.gridwidth = 2;
      gridBagConstraints11.insets = new Insets(0, 0, 0, 10);
      gridBagConstraints11.gridx = 1;
      GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
      gridBagConstraints8.fill = GridBagConstraints.HORIZONTAL;
      gridBagConstraints8.gridy = 4;
      gridBagConstraints8.weightx = 1.0;
      gridBagConstraints8.gridwidth = 3;
      gridBagConstraints8.insets = new Insets(0, 0, 0, 10);
      gridBagConstraints8.gridx = 1;
      GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
      gridBagConstraints7.fill = GridBagConstraints.HORIZONTAL;
      gridBagConstraints7.gridy = 2;
      gridBagConstraints7.gridwidth = 3;
      gridBagConstraints7.insets = new Insets(0, 0, 0, 10);
      gridBagConstraints7.gridx = 1;
      GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
      gridBagConstraints6.gridx = 0;
      gridBagConstraints6.insets = new Insets(0, 10, 0, 5);
      gridBagConstraints6.anchor = GridBagConstraints.WEST;
      gridBagConstraints6.gridy = 6;
      expireLabel = new JLabel();
      expireLabel.setText(Messages.getString("BasicElementDialog.9")); //$NON-NLS-1$
      GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
      gridBagConstraints5.gridx = 0;
      gridBagConstraints5.anchor = GridBagConstraints.WEST;
      gridBagConstraints5.insets = new Insets(0, 10, 0, 5);
      gridBagConstraints5.fill = GridBagConstraints.VERTICAL;
      gridBagConstraints5.gridy = 5;
      commentLabel = new JLabel();
      commentLabel.setText(Messages.getString("BasicElementDialog.10")); //$NON-NLS-1$
      commentLabel.setVerticalTextPosition(SwingConstants.TOP);
      commentLabel.setVerticalAlignment(SwingConstants.TOP);
      GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
      gridBagConstraints4.gridx = 0;
      gridBagConstraints4.anchor = GridBagConstraints.WEST;
      gridBagConstraints4.insets = new Insets(0, 10, 0, 5);
      gridBagConstraints4.gridy = 4;
      urlLabel = new JLabel();
      urlLabel.setText(Messages.getString("BasicElementDialog.11")); //$NON-NLS-1$
      GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
      gridBagConstraints3.gridx = 0;
      gridBagConstraints3.insets = new Insets(0, 10, 0, 5);
      gridBagConstraints3.anchor = GridBagConstraints.WEST;
      gridBagConstraints3.gridy = 3;
      passwordLabel = new JLabel();
      passwordLabel.setText(Messages.getString("BasicElementDialog.12")); //$NON-NLS-1$
      GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
      gridBagConstraints2.gridx = 0;
      gridBagConstraints2.insets = new Insets(0, 10, 0, 5);
      gridBagConstraints2.anchor = GridBagConstraints.WEST;
      gridBagConstraints2.gridy = 2;
      userLabel = new JLabel();
      userLabel.setText(Messages.getString("BasicElementDialog.13")); //$NON-NLS-1$
      GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
      gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
      gridBagConstraints1.gridy = 1;
      gridBagConstraints1.weightx = 1.0;
      gridBagConstraints1.gridx = 1;
      GridBagConstraints gridBagConstraints = new GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.insets = new Insets(0, 10, 0, 5);
      gridBagConstraints.anchor = GridBagConstraints.WEST;
      gridBagConstraints.gridy = 1;
      nameLabel = new JLabel();
      nameLabel.setText(Messages.getString("BasicElementDialog.14")); //$NON-NLS-1$
      mainPanel = new JPanel();
      mainPanel.setLayout(new GridBagLayout());
      mainPanel.add(nameLabel, gridBagConstraints);
      mainPanel.add(getNameTextField(), gridBagConstraints1);
      mainPanel.add(userLabel, gridBagConstraints2);
      mainPanel.add(passwordLabel, gridBagConstraints3);
      mainPanel.add(urlLabel, gridBagConstraints4);
      mainPanel.add(commentLabel, gridBagConstraints5);
      mainPanel.add(expireLabel, gridBagConstraints6);
      mainPanel.add(getUserTextField(), gridBagConstraints7);
      mainPanel.add(getUrlTextField(), gridBagConstraints8);
      mainPanel.add(getPasswordPasswordField(), gridBagConstraints11);
      mainPanel.add(symbolLabel, gridBagConstraints12);
      mainPanel.add(getSymbolComboBox(), gridBagConstraints13);
      mainPanel.add(getTooglePasswordButton(), gridBagConstraints14);
      mainPanel.add(spaceLabel, gridBagConstraints21);
      mainPanel.add(getCommentScrollPane(), gridBagConstraints31);
      mainPanel.add(getDateField(), gridBagConstraints15);
      mainPanel.add(getDateExpireCheckBox(), gridBagConstraints22);
    }
    return mainPanel;
  }

  /**
   * This method initializes nameTextField	
   * 	
   * @return javax.swing.JTextField	
   */
  private JTextField getNameTextField() {
    if (nameTextField == null) {
      nameTextField = new JTextField();
    }
    return nameTextField;
  }

  /**
   * This method initializes userTextField	
   * 	
   * @return javax.swing.JTextField	
   */
  private JTextField getUserTextField() {
    if (userTextField == null) {
      userTextField = new JTextField();
    }
    return userTextField;
  }

  /**
   * This method initializes urlTextField	
   * 	
   * @return javax.swing.JTextField	
   */
  private JTextField getUrlTextField() {
    if (urlTextField == null) {
      urlTextField = new JTextField();
    }
    return urlTextField;
  }

  /**
   * This method initializes commentTextArea	
   * 	
   * @return javax.swing.JTextArea	
   */
  private JTextArea getCommentTextArea() {
    if (commentTextArea == null) {
      commentTextArea = new JTextArea();
      commentTextArea.setAutoscrolls(true);
    }
    return commentTextArea;
  }

  /**
   * This method initializes passwordPasswordField	
   * 	
   * @return javax.swing.JPasswordField	
   */
  private JPasswordField getPasswordPasswordField() {
    if (passwordPasswordField == null) {
      passwordPasswordField = new JPasswordField();
    }
    return passwordPasswordField;
  }

  /**
   * This method initializes symbolComboBox	
   * 	
   * @return javax.swing.JComboBox	
   */
  private JComboBox getSymbolComboBox() {
    if (symbolComboBox == null) {
      symbolComboBox = new SymbolComboBox();
      symbolComboBox.setSelectedItem(IconPool.DEFAULT_ELEMENT_ID);
    }
    return symbolComboBox;
  }

  /**
   * This method initializes tooglePasswordButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getTooglePasswordButton() {
    if (tooglePasswordButton == null) {
      tooglePasswordButton = new JButton();
      tooglePasswordButton.setIcon(ICON_PASS_SHOW);
      tooglePasswordButton.setPreferredSize(new Dimension(30, 22));
      tooglePasswordButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          if (passwordVisible) {
            tooglePasswordButton.setIcon(ICON_PASS_SHOW);
            getPasswordPasswordField().setEchoChar('*');
          } else {
            tooglePasswordButton.setIcon(ICON_PASS_HIDE);
            getPasswordPasswordField().setEchoChar((char)0);
          }
          passwordVisible = !passwordVisible;
        }
      });
    }
    return tooglePasswordButton;
  }

  /**
   * This method initializes commentScrollPane	
   * 	
   * @return javax.swing.JScrollPane	
   */
  private JScrollPane getCommentScrollPane() {
    if (commentScrollPane == null) {
      commentScrollPane = new JScrollPane();
      commentScrollPane.setViewportView(getCommentTextArea());
    }
    return commentScrollPane;
  }

  /**
   * getBasicElement
   *
   * @return Returns the basicElement.
   */
  public BasicElement getBasicElement() {
    if (basicElement == null) basicElement = new BasicElement(null, null);
    basicElement.setName(nameTextField.getText());
    basicElement.setSymbol((String)symbolComboBox.getSelectedItem());
    basicElement.setComment(commentTextArea.getText());
    basicElement.setPassword(String.valueOf(passwordPasswordField.getPassword()));
    basicElement.setUrl(urlTextField.getText());
    basicElement.setUser(userTextField.getText());
    // for date
    if (dateExpireCheckBox.isSelected()) {
      basicElement.setExpire(null);
    } else {
      basicElement.setExpire((Date)dateField.getValue());
    }
    return basicElement;
  }

  /**
   * setBasicElement
   *
   * @param basicElement The basicElement to set.
   */
  public void setBasicElement(BasicElement basicElement) {
    this.basicElement = basicElement;
    nameTextField.setText(basicElement.getName());
    symbolComboBox.setSelectedItem(basicElement.getSymbol());
    commentTextArea.setText(basicElement.getComment());
    passwordPasswordField.setText(basicElement.getPassword());
    urlTextField.setText(basicElement.getUrl());
    userTextField.setText(basicElement.getUser());
    // for date
    if (basicElement.getExpire() == null) {
      dateExpireCheckBox.setSelected(true);
      dateField.setEnabled(false);
    } else {
      dateExpireCheckBox.setSelected(false);
      dateField.setBaseDate(basicElement.getExpire());
      dateField.setValue(basicElement.getExpire());
    }
  }

  /**
   * getResult
   *
   * @return Returns the result.
   */
  public int getResult() {
    return result;
  }

  /**
   * This method initializes dateField	
   * 	
   * @return net.sf.nachocalendar.components.DateField	
   */
  private DateField getDateField() {
    if (dateField == null) {
      dateField = new DateField(DateFormat.getDateInstance());
      dateField.setPreferredSize(new Dimension(100, 20));
      dateField.setEnabled(false);
    }
    return dateField;
  }

  /**
   * This method initializes dateExpireCheckBox	
   * 	
   * @return javax.swing.JCheckBox	
   */
  private JCheckBox getDateExpireCheckBox() {
    if (dateExpireCheckBox == null) {
      dateExpireCheckBox = new JCheckBox();
      dateExpireCheckBox.setText(Messages.getString("BasicElementDialog.15")); //$NON-NLS-1$
      dateExpireCheckBox.setSelected(true);
      dateExpireCheckBox.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent e) {
          if (e.getStateChange() == ItemEvent.SELECTED) {
            getDateField().setEnabled(false);
          } else {
            getDateField().setEnabled(true);
          }
        }
      });
    }
    return dateExpireCheckBox;
  }

  /**
   * This method initializes addToClipboardButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getAddToClipboardButton() {
    if (addToClipboardButton == null) {
      addToClipboardButton = new JButton();
      addToClipboardButton.setText(Messages.getString("BasicElementDialog.16")); //$NON-NLS-1$
      addToClipboardButton.setIcon(new ImageIcon(getClass().getResource("/org/javalobby/icons/20x20/Paste.gif")));
      addToClipboardButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          Utils.setClipboardContents(String.valueOf(passwordPasswordField.getPassword()));
        }
      });
    }
    return addToClipboardButton;
  }

}  //  @jve:decl-index=0:visual-constraint="10,10"
